import { observable, action, makeObservable } from 'mobx';
import moment from "moment";
import { currentUser } from "@/services/auth";
import GlobalFunctions from '@/store/globalFunctions';

class PaymentModel {
    @observable id = 0;//
    @observable bill_date = moment();//--申请日期
    @observable code = "";//--编码
    @observable name = "";//名称
    @observable customerid = "";//--客户ID
    @observable employeeid = "";
    @observable amount = 0;//金额
    @observable othertype = "";//其他费用
    @observable otheramount = 0;//其他费用
    @observable payname = "";//收款人
    @observable accountname = "";//户名
    @observable accountcode = "";//账户
    @observable accountbank = "";//开户行
    @observable paydate = "";//收付款日期
    @observable message = "";//--纪录事项
    @observable signingtime = null;//签约时间
    @observable expirationtime = null;//到期时间
    @observable status = 0;//--状态
    @observable orgunitid = currentUser.orgunit_id;
    @observable userid = currentUser.id;//--操作员ID
    @observable paymentactivitydata = [];
    @observable employeedata = [];
    @observable paytype=0;
    @observable page= 1;
    @observable pagesize= 20;
    @observable total= 1;
    @observable order= 1;
    //=========
    @observable isnew = true;
    @observable isedit = true;
    @observable allow_col = [];
    @observable colcontrol = false;

    constructor(id,paytype) {
        this.id = id;
        this.paytype=paytype;
        makeObservable(this);
    }
    @action setCode(code) {
        this.code = code;
    }
    @action setName(name) {
        this.name = name;
    }
    @action setBilledit(item) {
        this.isedit = item.edit;
        this.allow_col = item.allow_col;
        this.colcontrol = item.colcontrol;
    }
    @action setBill_date(bill_date) {
        this.bill_date = bill_date;
    }
    @action setCustomerid(customerid) {
        this.customerid = customerid;
    }
    @action setEmployeeid(employeeid) {
        this.employeeid = employeeid;
    }
    @action setAmount(amount) {
        this.amount = amount;
    }
    @action setOthertype(othertype) {
        this.othertype = othertype;
    }
    @action setOtheramount(otheramount) {
        this.otheramount = otheramount;
    }
    @action setPayname(payname) {
        this.payname = payname;
    }
    @action setAccountname(accountname) {
        this.accountname = accountname;
    }
    @action setAccountcode(accountcode) {
        this.accountcode = accountcode;
    }
    @action setAccountbank(accountbank) {
        this.accountbank = accountbank;
    }
    @action setPaydate(paydate) {
        this.paydate = paydate;
    }
    @action setMessage(message) {
        this.message = message;
    }
    @action setSigningtime(signingtime) {
        this.signingtime = signingtime;
    }
    @action setExpirationtime(expirationtime) {
        this.expirationtime = expirationtime;
    }
    @action setStatus(status) {
        this.status = status;
    }
    @action setOrgunitid(orgunitid) {
        this.orgunitid = orgunitid;
    }
    @action setUserid(userid) {
        this.userid = userid;
    }
    @action setPage(page) {
        this.page = page;
    }
    @action setPagesize(pagesize) {
        this.pagesize = pagesize;
    }
    @action setTotal(total) {
        this.total = total;
    }
    @action setOrder(order) {
        this.order = order;
    }
    @action setPaymentactivitydata(paymentactivitydata,paytype,projectdetailid) {
        this.paymentactivitydata = paymentactivitydata;
        if (paymentactivitydata.length > 0 && projectdetailid !== "0" && projectdetailid !== 0) {
            let item = this.employeedata.filter((p) => {
                return p.employeeid === paymentactivitydata[0].employeeid;
            });
            this.setinitdetaildata(item,paytype)
         }
    }
    @action setEmployeedata(employeedata,paytype,projectdetailid) {
        this.employeedata = employeedata;
        if (this.paymentactivitydata.length > 0 && projectdetailid !== "0" && projectdetailid !== 0) {
            let item = employeedata.filter((p) => {
                return p.employeeid === this.paymentactivitydata[0].employeeid;
            });
            this.setinitdetaildata(item,paytype)
         }
    }
    @action setinitdetaildata(item, paytype) {
        if (item.length > 0) {
            if (paytype === 0) {
                this.employeeid = item[0].employeeid;
                this.code = item[0].code;
                this.name = item[0].name;
                this.payname = item[0].name;
                this.accountname = item[0].accountname;
                this.accountcode = item[0].accountcode;
                this.accountbank = item[0].accountbank;
                this.amount = item[0].signamount;
                this.signingtime = GlobalFunctions.isEmptyobj(item[0].signingtime) ? null : moment(item[0].signingtime);
                this.expirationtime = GlobalFunctions.isEmptyobj(item[0].expirationtime) ? null : moment(item[0].expirationtime);
            } else {
                this.code = item[0].code;
                this.name = item[0].name;
                this.payname = item[0].name;
                this.amount = item[0].amount;
                this.signingtime = GlobalFunctions.isEmptyobj(item[0].signingtime) ? null : moment(item[0].signingtime);
                this.expirationtime = GlobalFunctions.isEmptyobj(item[0].expirationtime) ? null : moment(item[0].expirationtime);
                this.customerid = item[0].customerid;//--客户ID
            }
        }
    }

    static fromJS(obj,paytype) {
        let m = new PaymentModel(obj.id,paytype);
        //m.id= obj.id ;
        m.bill_date = moment(obj.bill_date);//--申请日期
        m.code = obj.code;//--编码
        m.name = obj.name;//名称
        m.customerid = obj.customerid;//客户ID
        m.employeeid = obj.employeeid;
        m.amount = obj.amount;
        m.othertype = obj.othertype;
        m.otheramount = obj.otheramount;
        m.payname = obj.payname;
        m.accountname = obj.accountname;
        m.accountcode = obj.accountcode;
        m.accountbank = obj.accountbank;
        m.paydate = obj.paydate ? (GlobalFunctions.isEmptyobj(obj.paydate) ? null : moment(obj.paydate)) : null;
        m.message = obj.message;
        m.signingtime = obj.signingtime ? (GlobalFunctions.isEmptyobj(obj.signingtime) ? null : moment(obj.signingtime)) : null;//签约时间
        m.expirationtime = obj.expirationtime ? (GlobalFunctions.isEmptyobj(obj.expirationtime) ? null : moment(obj.expirationtime)) : null;//到期时间
        m.status = obj.status;
        m.orgunitid = obj.orgunitid;
        m.userid = obj.userid;

        m.isnew = false;
        return m;
    }
}

export default PaymentModel;